﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace ElingOA6_DSJCJ.DAL
{
    /// <summary>
    /// Document
    /// </summary>
    public partial class Document
    {
        public Document()
        {
        }
        /// <summary>
        /// 获得单个缩略图路径
        /// </summary>
        /// <param name="entityNo">公司编号</param>
        /// <param name="docId">信息编号</param>
        /// <returns>缩略图路径</returns>
        public string GetDocFThumbSrvFileName(string entityNo, decimal docId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FThumbSrvFileName from t_document ");
            strSql.Append(" where FEntityNo=@FEntityNo and FDocId=@FDocId ");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FDocId", SqlDbType.Decimal)};
            parameters[0].Value = entityNo;
            parameters[1].Value = docId;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj != null)
            {
                return obj.ToString();
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 获得该类别下前多少条数据
        /// </summary>
        /// <param name="entityNo">公司编号</param>
        /// <param name="fDocClsCd">类别编号</param>
        /// <param name="top">多少条数据 0为全部</param>
        /// <returns>信息List</returns>
        public List<Model.Document> GetALLListByFDocClsCd(string entityNo, string fDocClsCd, int top)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select");
            if (top != 0)
            {
                strSql.Append(" top " + top);
            }
            strSql.Append(" FDocId,FTitle,FPubDate,FEmplNo,FDeptName from v_document ");
            strSql.Append(" where FEntityNo=@FEntityNo and FDocClsCd=@FDocClsCd AND FRecycle='N' AND FIsEnroll='Y' order by FPubDate desc");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FDocClsCd", SqlDbType.VarChar,25)};
            parameters[0].Value = entityNo;
            parameters[1].Value = fDocClsCd;

            DataSet obj = DbHelperSQL.Query(strSql.ToString(), parameters);

            List<Model.Document> documentList = new List<Model.Document>();
            for (int i = 0; i < obj.Tables[0].Rows.Count; i++)
            {
                Model.Document documentModel = new Model.Document();
                documentModel.FTitle = obj.Tables[0].Rows[i]["FTitle"].ToString();
                documentModel.FDocId = decimal.Parse(obj.Tables[0].Rows[i]["FDocId"].ToString());
                documentModel.FPubDate = DateTime.Parse(obj.Tables[0].Rows[i]["FPubDate"].ToString());
                documentModel.FEmplNo = obj.Tables[0].Rows[i]["FEmplNo"].ToString();
                documentModel.FDeptName = obj.Tables[0].Rows[i]["FDeptName"].ToString();
                documentList.Add(documentModel);
            }
            return documentList;
           
        }
       

        /// <summary>
        /// 获取文档实体类
        /// <creator>wangl</creator>
        /// <date>2013-08-07</date> 
        /// </summary>
        /// <param name="docId">文档编号</param>
        /// <returns>文档实体类</returns>
        public Model.Document GetModel(decimal docId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  top 1 FDocId,FDocClsCd,FEntityNo,FDocSrcName,FPubDate,FPubIp,FTitle,FSubTitle,FContentTxt,FContentBin,FSvrFileName,FAuthor,FReadCnt,FThumbGenType,FThumbContent,FThumbFileExt,FThumbSrvFileName,FThumbOldFileName,FThumbSeqID,FEditMode,FIsTop,FTopSeq,FIsEnroll,FStatus,FIsShowHomePage,FDeptNo,FEmplNo,FAuditDeptNo,FAuditEmpNo,FValidDate,FReferenceNo,FWorkGrpNo,FAuditDate,FAuditIP,FUpdDeptNo,FUpdEmplNo,FUpdDate,FUpdIP,FRecycle,FDelEmplNo,FDelDate,FIsPubJCMS,FParallelDocClsCd,FExamineDeptNo,FExamineEmpNo,FExamineDate,FExamineIP,FExamineContent,FExamineCase,FCaseMemo,FSuperiorApply,FSuperiorApplyMemo,FContentIsImg,FDownTitle,FDownDateString from t_document ");
            strSql.Append(" WHERE FDocId=@FDocId ");
            SqlParameter[] parameters = {
					new SqlParameter("@FDocId", SqlDbType.Decimal)};
            parameters[0].Value = docId;
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        ///  判断当前登陆人员是否有权限对指定文档的查看权限
        /// <creator>wangl</creator>
        /// <date>2013-09-06</date>
        /// </summary>
        /// <param name="emplNo"></param>
        /// <param name="docClsCd"></param>
        /// <param name="entityNo"></param>
        /// <param name="docId"></param>
        /// <returns></returns>
        //public bool IsDocAuthorityForEmpl(string emplNo, string docClsCd, string entityNo, decimal docId)
        //{
        //    StringBuilder strSql = new StringBuilder();
        //    strSql.Append("select count(1) FROM v_Document a inner join t_DWDocClsDestAuthority b ");
        //    strSql.Append(" on a.FEntityNo = b.FEntityNo and ");
        //    strSql.Append(" a.FDocClsCd = b.FDocClsCd and ");
        //    strSql.Append("( b.FEmplNo='#ALL#' or (");
        //    strSql.Append(" (a.FDeptNo = b.FDestDeptNo or a.FWorkGrpNo = b.FDestWorkGrpNo) ");
        //    strSql.Append(" and b.FEmplNo=@FEmplNo) ) where a.FDocClsCd=@FDocClsCd and a.FEntityNo=@FEntityNo and a.FDocId=@FDocId and a.FIsEnroll='Y' and a.FRecycle='N' ");

        //    SqlParameter[] parameters = {
        //            new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15),
        //            new SqlParameter("@FDocClsCd", SqlDbType.VarChar,50),
        //            new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50),
        //            new SqlParameter("@FDocId", SqlDbType.Decimal)};
        //    parameters[0].Value = emplNo;
        //    parameters[1].Value = docClsCd;
        //    parameters[2].Value = entityNo;
        //    parameters[3].Value = docId;

        //    return DbHelperSQL.Exists(strSql.ToString(), parameters);
        //}
        /// <summary>
        ///  判断当前登陆人员是否有权限对指定文档的查看权限
        /// <creator>wangl</creator>
        /// <date>2013-09-06</date>
        /// </summary>
        /// <param name="emplNo"></param>
        /// <param name="docClsCd"></param>
        /// <param name="entityNo"></param>
        /// <param name="docId"></param>
        /// <returns></returns>
        public bool IsDocAuthorityForEmpl(string emplNo, string docClsCd, string entityNo, decimal docId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM v_Document a inner join t_DWDocClsDestAuthority b ");
            strSql.Append(" on a.FEntityNo = b.FEntityNo and ");
            strSql.Append(" a.FDocClsCd = b.FDocClsCd and ");
            strSql.Append("( b.FEmplNo='#ALL#' or (");
            strSql.Append(" (a.FDeptNo = b.FDestDeptNo or a.FWorkGrpNo = b.FDestWorkGrpNo) ");
            strSql.Append(" and b.FEmplNo=@FEmplNo) ) AND FAuthorityType='LOOK' where a.FDocClsCd=@FDocClsCd and a.FEntityNo=@FEntityNo and a.FDocId=@FDocId and a.FIsEnroll='Y' and a.FRecycle='N' ");

            SqlParameter[] parameters = {
                    new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FDocClsCd", SqlDbType.VarChar,50),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FDocId", SqlDbType.Decimal)};
            parameters[0].Value = emplNo;
            parameters[1].Value = docClsCd;
            parameters[2].Value = entityNo;
            parameters[3].Value = docId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        ///  判断当前登陆人员是否有权限对指定文档的查看权限
        /// <creator>sunlj</creator>
        /// <date>2014-06-17</date>
        /// </summary>
        /// <param name="emplNo"></param>
        /// <param name="docId"></param>
        /// <returns></returns>
        public bool IsDocumentAuthorityForEmpl(string emplNo, decimal docId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select count(1) FROM v_Document a inner join dbo.t_DWDocumentDestAuthority b ");
            strSql.Append(" ON a.FDocId = b.FDocId and ");
            strSql.Append(" ( b.FEmplNo='#ALL#' or ( ");
            strSql.Append(" (ISNULL(a.FDeptNo,'null') =ISNULL(b.FDestDeptNo,'null') or ISNULL(a.FWorkGrpNo,'null') = ISNULL(b.FDestWorkGrpNo,'null'))  ");
            strSql.Append("  and b.FEmplNo=@FEmplNo) ) where a.FDocId=@FDocId and a.FIsEnroll='Y' and a.FRecycle='N' ");

            SqlParameter[] parameters = {
                    new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FDocId", SqlDbType.Decimal)};
            parameters[0].Value = emplNo;
            parameters[1].Value = docId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 判断当前信息是否所有人可见
        /// <creator>sunlj</creator>
        /// <date>2014-06-17</date>
        /// </summary>
        /// <param name="docId"></param>
        /// <returns></returns>
        public bool IsDocumentAuthorityForEveryone(decimal docId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select count(1) FROM v_Document a inner join dbo.t_DWDocumentDestAuthority b ");
            strSql.Append(" ON a.FDocId = b.FDocId and ");
            strSql.Append(" b.FEmplNo='#EVERYONE#' ");
            strSql.Append(" where a.FDocId=@FDocId and a.FIsEnroll='Y' and a.FRecycle='N' ");

            SqlParameter[] parameters = {
					new SqlParameter("@FDocId", SqlDbType.Decimal)};
            parameters[0].Value = docId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 更新浏览次数
        /// <creator>wangl</creator>
        /// <date>2013-09-16</date>
        /// </summary>
        /// <param name="docId"></param>
        public void UpdateReadCntByDocId(decimal docId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE t_Document set ");
            strSql.Append("FReadCnt=FReadCnt+1");
            strSql.Append(" where FDocId=@FDocId ");
            SqlParameter[] parameters = {
					new SqlParameter("@FDocId", SqlDbType.Decimal,9)};
            parameters[0].Value = docId;
            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_DSJCJ.Model.Document DataRowToModel(DataRow row)
        {
            ElingOA6_DSJCJ.Model.Document model = new ElingOA6_DSJCJ.Model.Document();
            if (row != null)
            {
                if (row["FDocId"] != null && row["FDocId"].ToString() != "")
                {
                    model.FDocId = decimal.Parse(row["FDocId"].ToString());
                }
                if (row["FDocClsCd"] != null)
                {
                    model.FDocClsCd = row["FDocClsCd"].ToString();
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                //if (row["FDocSrcName"] != null)
                //{
                //    model.FDocSrcName = row["FDocSrcName"].ToString();
                //}
                if (row["FPubDate"] != null && row["FPubDate"].ToString() != "")
                {
                    model.FPubDate = DateTime.Parse(row["FPubDate"].ToString());
                }
                if (row["FPubIp"] != null)
                {
                    model.FPubIp = row["FPubIp"].ToString();
                }
                if (row["FTitle"] != null)
                {
                    model.FTitle = row["FTitle"].ToString();
                }
                if (row["FSubTitle"] != null)
                {
                    model.FSubTitle = row["FSubTitle"].ToString();
                }
                if (row["FContentTxt"] != null)
                {
                    model.FContentTxt = row["FContentTxt"].ToString();
                }
                if (row["FContentBin"] != null && row["FContentBin"].ToString() != "")
                {
                    model.FContentBin = (byte[])row["FContentBin"];
                }
                if (row["FSvrFileName"] != null)
                {
                    model.FSvrFileName = row["FSvrFileName"].ToString();
                }
                if (row["FAuthor"] != null)
                {
                    model.FAuthor = row["FAuthor"].ToString();
                }
                if (row["FReadCnt"] != null && row["FReadCnt"].ToString() != "")
                {
                    model.FReadCnt = int.Parse(row["FReadCnt"].ToString());
                }
                if (row["FThumbGenType"] != null)
                {
                    model.FThumbGenType = row["FThumbGenType"].ToString();
                }
                if (row["FThumbContent"] != null && row["FThumbContent"].ToString() != "")
                {
                    model.FThumbContent = (byte[])row["FThumbContent"];
                }
                if (row["FThumbFileExt"] != null)
                {
                    model.FThumbFileExt = row["FThumbFileExt"].ToString();
                }
                if (row["FThumbSrvFileName"] != null)
                {
                    model.FThumbSrvFileName = row["FThumbSrvFileName"].ToString();
                }
                if (row["FThumbOldFileName"] != null)
                {
                    model.FThumbOldFileName = row["FThumbOldFileName"].ToString();
                }
                if (row["FThumbSeqID"] != null && row["FThumbSeqID"].ToString() != "")
                {
                    model.FThumbSeqID = int.Parse(row["FThumbSeqID"].ToString());
                }
                if (row["FEditMode"] != null)
                {
                    model.FEditMode = row["FEditMode"].ToString();
                }
                if (row["FIsTop"] != null && row["FIsTop"].ToString() != "")
                {
                    model.FIsTop = int.Parse(row["FIsTop"].ToString());
                }
                if (row["FTopSeq"] != null && row["FTopSeq"].ToString() != "")
                {
                    model.FTopSeq = int.Parse(row["FTopSeq"].ToString());
                }
                if (row["FIsEnroll"] != null)
                {
                    model.FIsEnroll = row["FIsEnroll"].ToString();
                }
                if (row["FStatus"] != null)
                {
                    model.FStatus = row["FStatus"].ToString();
                }
                if (row["FIsShowHomePage"] != null)
                {
                    model.FIsShowHomePage = row["FIsShowHomePage"].ToString();
                }
                if (row["FDeptNo"] != null)
                {
                    model.FDeptNo = row["FDeptNo"].ToString();
                }
                if (row["FEmplNo"] != null)
                {
                    model.FEmplNo = row["FEmplNo"].ToString();
                }
                if (row["FAuditDeptNo"] != null)
                {
                    model.FAuditDeptNo = row["FAuditDeptNo"].ToString();
                }
                if (row["FAuditEmpNo"] != null)
                {
                    model.FAuditEmpNo = row["FAuditEmpNo"].ToString();
                }
                if (row["FValidDate"] != null && row["FValidDate"].ToString() != "")
                {
                    model.FValidDate = DateTime.Parse(row["FValidDate"].ToString());
                }
                if (row["FReferenceNo"] != null)
                {
                    model.FReferenceNo = row["FReferenceNo"].ToString();
                }
                if (row["FWorkGrpNo"] != null)
                {
                    model.FWorkGrpNo = row["FWorkGrpNo"].ToString();
                }
                if (row["FAuditDate"] != null && row["FAuditDate"].ToString() != "")
                {
                    model.FAuditDate = DateTime.Parse(row["FAuditDate"].ToString());
                }
                if (row["FAuditIP"] != null)
                {
                    model.FAuditIP = row["FAuditIP"].ToString();
                }
                if (row["FUpdDeptNo"] != null)
                {
                    model.FUpdDeptNo = row["FUpdDeptNo"].ToString();
                }
                if (row["FUpdEmplNo"] != null)
                {
                    model.FUpdEmplNo = row["FUpdEmplNo"].ToString();
                }
                if (row["FUpdDate"] != null && row["FUpdDate"].ToString() != "")
                {
                    model.FUpdDate = DateTime.Parse(row["FUpdDate"].ToString());
                }
                if (row["FUpdIP"] != null)
                {
                    model.FUpdIP = row["FUpdIP"].ToString();
                }
                if (row["FRecycle"] != null)
                {
                    model.FRecycle = row["FRecycle"].ToString();
                }
                if (row["FDelEmplNo"] != null)
                {
                    model.FDelEmplNo = row["FDelEmplNo"].ToString();
                }
                if (row["FDelDate"] != null && row["FDelDate"].ToString() != "")
                {
                    model.FDelDate = DateTime.Parse(row["FDelDate"].ToString());
                }
                if (row["FIsPubJCMS"] != null)
                {
                    model.FIsPubJCMS = row["FIsPubJCMS"].ToString();
                }
                if (row["FParallelDocClsCd"] != null)
                {
                    model.FParallelDocClsCd = row["FParallelDocClsCd"].ToString();
                }
                if (row["FExamineDeptNo"] != null)
                {
                    model.FExamineDeptNo = row["FExamineDeptNo"].ToString();
                }
                if (row["FExamineEmpNo"] != null)
                {
                    model.FExamineEmpNo = row["FExamineEmpNo"].ToString();
                }
                if (row["FExamineDate"] != null && row["FExamineDate"].ToString() != "")
                {
                    model.FExamineDate = DateTime.Parse(row["FExamineDate"].ToString());
                }
                if (row["FExamineIP"] != null)
                {
                    model.FExamineIP = row["FExamineIP"].ToString();
                }
                if (row["FExamineContent"] != null)
                {
                    model.FExamineContent = row["FExamineContent"].ToString();
                }
                if (row["FExamineCase"] != null)
                {
                    model.FExamineCase = row["FExamineCase"].ToString();
                }
                if (row["FCaseMemo"] != null)
                {
                    model.FCaseMemo = row["FCaseMemo"].ToString();
                }
                if (row["FSuperiorApply"] != null)
                {
                    model.FSuperiorApply = row["FSuperiorApply"].ToString();
                }
                if (row["FSuperiorApplyMemo"] != null)
                {
                    model.FSuperiorApplyMemo = row["FSuperiorApplyMemo"].ToString();
                }
                if (row["FContentIsImg"] != null)
                {
                    model.FContentIsImg = row["FContentIsImg"].ToString();
                }
                if (row.Table.Columns.Contains("FDownTitle"))
                {
                    if (row["FDownTitle"]!=null)
                    {
                        model.FDownTitle = row["FDownTitle"].ToString();
                    }
                }
                if (row.Table.Columns.Contains("FDownDateString"))
                {
                    if (row["FDownDateString"]!=null)
                    {
                        model.FDownDateString = row["FDownDateString"].ToString();
                    }
                }
            }
            return model;
        }
        /// <summary>
        /// 根据信息编号获取信息编号(适用于单信息)
        /// <creator>wangl</creator>
        /// <date>2013-09-10</date> 
        /// </summary>
        /// <param name="entityNo">公司编号</param>
        /// <param name="docClsCd">信息类别编号</param>
        /// <returns></returns>
        public decimal GetDocIdDocClsCd(string entityNo, string docClsCd)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 FDocId from t_document");
            strSql.Append(" where FEntityNo=@FEntityNo and FDocClsCd=@FDocClsCd");
            SqlParameter[] parameters = {
                    new SqlParameter("@FDocClsCd", SqlDbType.VarChar,50),
                    new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50)};
            parameters[0].Value = docClsCd;
            parameters[1].Value = entityNo;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj != null)
            {
                return decimal.Parse(obj.ToString());
            }
            else
            {
                return 0;
            }
        }


        /// <summary>
        /// 更新批示状态
        /// </summary>
        /// <param name="FDocClsCd"></param>
        /// <param name="FEntityNo"></param>
        public void UpdateFisInstruct(string fdocId, string FEntityNo, string charVal)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_document set ");
            strSql.Append("FIsInstruct=@FIsInstruct");
            strSql.Append(" where FDocId=@FDocId and FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FDocId", SqlDbType.NVarChar,30),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
                    new SqlParameter("@FIsInstruct", SqlDbType.Char,1)};
            parameters[0].Value = fdocId;
            parameters[1].Value = FEntityNo;
            parameters[2].Value = charVal;
            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

       

    }
}
